package com.freemodbus4j.network.handler;

import com.freemodbus4j.core.codec.ascii.ASCIIModbusCodec;
import com.freemodbus4j.core.frame.ASCIIFrame;
import com.freemodbus4j.core.frame.ModbusFrame;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;

import java.util.List;


public class ASCIIMessageCodec extends ByteToMessageCodec<ModbusFrame> {

    protected void encode(ChannelHandlerContext channelHandlerContext, ModbusFrame modbusFrame, ByteBuf byteBuf) throws Exception {
        ASCIIModbusCodec asciiModbusCodec = new ASCIIModbusCodec();
        asciiModbusCodec.encode((ASCIIFrame.Response)modbusFrame, byteBuf);
    }

    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        ASCIIModbusCodec asciiModbusCodec = new ASCIIModbusCodec();
        ModbusFrame frame = asciiModbusCodec.decode(byteBuf);
        list.add(frame);
    }
}
